package com.food.controller;

import com.food.entity.dto.FoodFavoriteDto;
import com.food.entity.pojo.FoodFavorite;
import com.food.service.FoodFavoriteService;
import com.food.common.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/favorites")
public class FoodFavoriteController {

    @Autowired
    private FoodFavoriteService foodFavoriteService;

    // 1. 添加收藏（使用 DTO 参数）
    @PostMapping
    public R addFavorite(@RequestBody FoodFavoriteDto dto) {
        foodFavoriteService.addFavorite(dto);
        return R.success(null);
    }

    // 2. 获取用户所有收藏
    @GetMapping("/user/{userId}")
    public R<List<FoodFavorite>> getFavoritesByUserId(@PathVariable Integer userId) {
        List<FoodFavorite> favorites = foodFavoriteService.getFavoritesByUserId(userId);
        return R.success(favorites);
    }

    // 3. 取消收藏（根据用户ID和美食ID）
    @DeleteMapping
    public R deleteFavorite(@RequestParam Integer userId, @RequestParam Integer foodId) {
        foodFavoriteService.deleteFavorite(userId, foodId);
        return R.success(null);
    }

    // 4. 创建收藏记录（直接传入实体类，用于调试或基础测试）
    @PostMapping("/create")
    public R createFoodFavorite(@RequestBody FoodFavorite foodFavorite) {
        foodFavoriteService.save(foodFavorite);
        return R.success(null);
    }

    // 5. 获取所有收藏记录（调试用）
    @GetMapping("/all")
    public R<List<FoodFavorite>> getAllFavorites() {
        return R.success(foodFavoriteService.list());
    }

    // 6. 根据ID获取收藏记录
    @GetMapping("/{id}")
    public R<FoodFavorite> getFavoriteById(@PathVariable Integer id) {
        return R.success(foodFavoriteService.getById(id));
    }

    // 7. 根据ID删除收藏记录
    @DeleteMapping("/{id}")
    public R deleteFavoriteById(@PathVariable Integer id) {
        foodFavoriteService.removeById(id);
        return R.success(null);
    }
}
